<?php
/*
 * 缴费管理
 */
namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\push;


class PaymentController extends  Controller
{

    private $app_key = '09ae7f39eb877836195b52df';        //待发送的应用程序(appKey)，只能填一个。
    private $master_secret ='28783d0dacf52cfd12d952a6';    //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址


    private $appios_key = '1aef5ddc66c9bc4e821e84a3';        //待发送的应用程序(appKey)，只能填一个。
    private $masterios_secret ='e0a01a1f2e56db54bf06f3fb';    //主密码
    private $urlios = "https://api.jpush.cn/v3/push";      //推送的地址

    //物业费全部金额
    public function sum($id,$type){
        switch ($type){
            case "1":
                $data       = DB::table('strata_fee')->where('user_stratafee',$id) ->where('state',0)->sum('money');
                return      $data;
            case "0":
                $data       = DB::table('strata_fee')->where('user_stratafee',$id) ->where('state',1)->sum('money');
                return      $data;

        }

    }
    //停车费全部金额
    public function parking($id,$type){
        switch ($type){
            case "1":
                $data       = DB::table('parking_fees')->where('user_parkingfees',$id) ->where('parking_state',0)->sum('parking_money');
                return      $data;

            case "0":
                $data       = DB::table('parking_fees')->where('user_parkingfees',$id) ->where('parking_state',1)->sum('parking_money');
                return      $data;

            default:
                break;
        }

    }

    //全部物业费
    public function AllPayment(Request $request,$type)
    {
        if ($request->isMethod('post')) {

            $village_id                       = $request ->input('village_id');
            if($type == 2) {
                $data = DB::table('strata_fee')
                    ->select('strata_fee_id', 'user_stratafee', 'floor', 'number', 'room', 'state','user_roomname')
                    ->leftjoin('user', 'strata_fee.user_stratafee', '=', 'user.user_id')
                    ->where('user_village', $village_id)
                  //  ->where('state',1)
                    ->get();
                foreach ($data as $row) {
                    $id = $row->strata_fee_id;
                    $state = $row->state;
                    $key = $row->user_stratafee;
                    $money  = $this ->sum($key,1);
                    $mon    = $this ->sum($key,0);
                    $tmpArray[$key]['id'] = $id;
                    $tmpArray[$key]['state'] = $state;
                    $tmpArray[$key]['money'] = $money+$mon;
                    $tmpArray[$key]['user_id'] = $key;
                    $tmpArray[$key]['state'] = $state;
                    $tmpArray[$key]['address'] = $row->user_roomname;
                    $da = array_values($tmpArray);
                }
                if ($data == true) {
                    return json::json('success', '获取物业费成功', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', []);
                }
            }else{
                $data = DB::table('user')
                    ->select('strata_fee_id', 'user_stratafee', 'floor', 'number', 'room', 'state','user_roomname')
                    ->leftjoin('strata_fee', 'strata_fee.user_stratafee', '=', 'user.user_id')
                    ->where('user_village', $village_id)
                    ->where('state',$type)
                    ->get();
                foreach ($data as $row) {
                    $id = $row->strata_fee_id;
                    $floor = $row->floor;
                    $number = $row->number;
                    $room = $row->room;
                    $state = $row->state;
                    $key = $row->user_stratafee;
                    $money  = $this ->sum($key,$type);
                    $key = $row->user_stratafee;
                    $tmpArray[$key]['id'] = $id;
                    $tmpArray[$key]['state'] = $state;
                    $tmpArray[$key]['user_id'] = $key;
                    $tmpArray[$key]['state'] = $state;
                    $tmpArray[$key]['money'] = $money;
                    $tmpArray[$key]['address'] = $row->user_roomname;
                    $da = array_values($tmpArray);
                }
                if ($data == true) {
                    return json::json('success', '获取物业费成功', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', []);
                }
            }
        } else {
            return json::json('101', '提交方式错误', '');
        }
    }

    public function MonthPayment(Request $request,$type)
    {
        if ($request->isMethod('post')) {
            $user_id = $request->input('user_id');
            if($type ==2) {
                $data = DB::table('strata_fee')
                    ->where('user_stratafee', $user_id)
                    ->get();
                foreach ($data as $value) {
                    $strata_fee_id = $value->strata_fee_id;
                    $years = $value->years;
                    $money = $value->money;
                    $state = $value->state;
                    $da[] = array('strata_fee_id' => $strata_fee_id, 'years' => $years, 'money' => $money, 'state' => $state);
                }
                if ($data == true) {
                    return json::json('success', '获取用户下的物业费', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }else{
                $data = DB::table('strata_fee')
                    ->where('user_stratafee', $user_id)
                    ->where('state',$type)
                    ->get();
                foreach ($data as $value) {
                    $strata_fee_id = $value->strata_fee_id;
                    $years = $value->years;
                    $money = $value->money;
                    $state = $value->state;
                    $da[] = array('strata_fee_id' => $strata_fee_id, 'years' => $years, 'money' => $money, 'state' => $state);
                }
                if ($data == true) {
                    return json::json('success', '获取用户下的物业费', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }
        } else {
            return json::json('101', '提交方式错误', '');
        }
    }

    public function SelectPayment(Request $request){
        if($request ->isMethod('post')){
            $strata_fee_id     = $request ->input('strata_fee_id');
            $data              = DB::table('strata_fee')
                               ->leftjoin('user', 'strata_fee.user_stratafee', '=', 'user.user_id')
                               ->where('strata_fee_id',$strata_fee_id)
                               ->get();
            foreach ($data as $value){
                $floor = $value->floor;
                $number = $value->number;
                $room = $value->room;
                $address = $value->user_roomname;
                $money   = $value ->money;
                $years   = $value ->years;
                $da      = array('years'=>$years,'address'=>$address,'money'=>$money,'sum'=>$money);
            }
            if ($data == true) {
                return json::json('success', '获取用户下的物业费', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        } else {
            return json::json('101', '提交方式错误', '');
        }
    }

    //全部停车费
    public function AllFees(Request $request,$type)
    {
        if ($request->isMethod('post')) {
            $village_id                       = $request ->input('village_id');
            if($type == 2) {
                $data = DB::table('parking_fees')
                    ->select('parking_fees_id', 'user_parkingfees', 'floor', 'number', 'room', 'parking_state','user_roomname')
                    ->leftjoin('user', 'parking_fees.user_parkingfees', '=', 'user.user_id')
                    ->where('user_village', $village_id)
                    ->get();
                foreach ($data as $row) {
                    $id = $row->parking_fees_id;
                    $floor = $row->floor;
                    $number = $row->number;
                    $room = $row->room;
                    $state = $row->parking_state;
                    $key = $row->user_parkingfees;
                    $money  = $this ->parking($key,1);
                    $mon    = $this  ->parking($key,0);
                    $tmpArray[$key]['id'] = $id;
                    $tmpArray[$key]['state'] = $state;
                    $tmpArray[$key]['money'] = $money+$mon;
                    $tmpArray[$key]['user_id'] = $key;
                    $tmpArray[$key]['state'] = $state;
                    $tmpArray[$key]['address'] = $row->user_roomname;
                    $da = array_values($tmpArray);
                }
                if ($data == true) {
                    return json::json('success', '获取停车费成功', $da);
                } else {
                    return json::json('failure', '获取停车费失败', '');
                }
            }else{
                $data = DB::table('user')
                    ->select('parking_fees_id', 'user_parkingfees', 'floor', 'number', 'room', 'parking_state','user_roomname','parking_money')
                    ->leftjoin('parking_fees', 'parking_fees.user_parkingfees', '=', 'user.user_id')
                    ->where('user_village', $village_id)
                    ->where('parking_state',$type)
                    ->get();
                foreach ($data as $row) {
                    $id = $row->parking_fees_id;
                    $floor = $row->floor;
                    $number = $row->number;
                    $room = $row->room;
                    $state = $row->parking_state;
                    $key = $row->user_parkingfees;
                    $money  = $row ->parking_money;
                    $tmpArray[$key]['id'] = $id;
                    $tmpArray[$key]['state'] = $state;
                    $tmpArray[$key]['user_id'] = $key;
                    $tmpArray[$key]['money'] = $money;
                    $tmpArray[$key]['state'] = $state;
                    $tmpArray[$key]['address'] = $row->user_roomname/*floor . $number . $room*/;
                    $da = array_values($tmpArray);
                }
                if ($data == true) {
                    return json::json('success', '获取停车费成功', $da);
                } else {
                    return json::json('failure', '获取停车费失败', '');
                }
            }
        } else {
            return json::json('101', '提交方式错误', '');
        }
    }


    //催缴
    public function Reminder(Request $request){
        if($request ->isMethod('post')) {
            $user_id = $request->input('user_id');
            if (empty($user_id)) {
                $arr = (object)array();
                return json::json('failure', '失败', $arr);
            } else {
                $uu        = rtrim($user_id,",");
                $array_ids = explode(',', $uu);

                $result = array();
                foreach ($array_ids as $k => $v) {
                    $res = DB::table('user')->select('phone')->where('user_id', '=', $v)->first();
                    $result[] = $res->phone;
                }
                $arr = (object)array();
                echo json::json('success', '物业费催成功', $arr);
                try {
                    $conten = "您有一条物业费未缴信息";
                    $push = new push($this->app_key, $this->master_secret, $this->url);
                    $pushios = new push($this->appios_key, $this->masterios_secret, $this->urlios);
                    $push->send_pubp($result, $conten, '3');
                    $pushios->send_pubp($result, $conten, '3');
                }catch (\Exception $exception){
                    exit();
                }
            }
        }
    }
    //催缴
    public function Tcreminder(Request $request){
        if($request ->isMethod('post')) {
            $user_id = $request->input('user_id');
            if (empty($user_id)) {
                $arr = (object)array();
                return json::json('failure', '失败', $arr);
            } else {
                $uu        = rtrim($user_id,",");
                $array_ids = explode(',', $uu);

                $result = array();
                foreach ($array_ids as $k => $v) {
                    $res = DB::table('user')->select('phone')->where('user_id', '=', $v)->first();
                    $result[] = $res->phone;
                }
                $arr = (object)array();
                echo json::json('success', '停车费催成功', $arr);
                try {
                    $conten = "您有一条停车费未缴信息";
                    $push = new push($this->app_key, $this->master_secret, $this->url);
                    $pushios = new push($this->appios_key, $this->masterios_secret, $this->urlios);
                    $push->send_pubp($result, $conten, '3');
                    $pushios->send_pubp($result, $conten, '3');
                }catch (\Exception $exception){
                    exit();
                }
            }
        }
    }

    public function MonthPayments(Request $request,$type)
    {
        if ($request->isMethod('post')) {
            $user_id = $request->input('user_id');
            if($type ==2) {
                $data = DB::table('parking_fees')
                    ->where('user_parkingfees', $user_id)
                    ->get();
                foreach ($data as $value) {
                    $strata_fee_id = $value->parking_fees_id;
                    $years = $value->parking_set;
                    $time  = explode('.',$years);
                    $a     = $time[0].'至'.$time[1];
                    $money = $value->parking_money;
                    $state = $value->parking_state;
                    $da[] = array('parking_fees_id' => $strata_fee_id, 'parking_set' => $a, 'money' => $money, 'parking_state' => $state);
                }
                if ($data == true) {
                    return json::json('success', '获取用户下的停车费', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }else{
                $data = DB::table('parking_fees')
                    ->where('user_parkingfees', $user_id)
                    ->where('user_parkingfees',$type)
                    ->get();
                foreach ($data as $value) {

                    $strata_fee_id = $value->parking_fees_id;
                    $years = $value->parking_set;
                    $time  = explode('.',$years);
                    $a     = $time[0].'至'.$time[1];
                    $money = $value->parking_money;
                    $state = $value->parking_state;
                    $da[] = array('parking_fees_id' => $strata_fee_id, 'parking_set' => $a, 'money' => '151', 'parking_state' => $state);
                }
                if ($data == true) {
                    return json::json('success', '获取用户下的停车费', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }
        } else {
            return json::json('101', '提交方式错误', '');
        }
    }

    public function SelectPayments(Request $request){
        if($request ->isMethod('post')){

            $strata_fee_id     = $request ->input('parking_fees_id');
            $data              = DB::table('parking_fees')
                ->leftjoin('user', 'parking_fees.user_parkingfees', '=', 'user.user_id')
                ->where('parking_fees_id',$strata_fee_id)
                ->get();
            foreach ($data as $value){
                $floor = $value->floor;
                $number = $value->number;
                $room = $value->room;
                $address = $value->user_roomname;
                $money   = $value ->parking_money;
                $years   = $value ->parking_set;
                $da      = array('years'=>$years,'address'=>$address,'money'=>$money,'sum'=>$money);
            }
            if ($data == true) {
                return json::json('success', '获取用户下的物业费', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        } else {
            return json::json('101', '提交方式错误', '');
        }
    }



}